﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;

namespace BLL
{
    /// <summary>
    /// 控件绑定类
    /// </summary>
    public static class PublicControlBind
    {
        /// <summary>
        /// 获取分析相关信息HTML字符串
        /// </summary>
        /// <param name="total"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static string GetPagingInfo(int total, int pageIndex, int pageSize)
        {
            int pageCount;
            if (total % pageSize == 0)
            {
                pageCount = total / pageSize;
            }
            else
            {
                pageCount = total / pageSize + 1;
            }
            if (total == 0)
            {
                pageIndex = 0;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("共检索到信息" + total.ToString() + "条&nbsp;");
            //sb.Append("页次" + pageIndex.ToString() + "<span style=\"color: blue\">/</span>");
            sb.Append("页次" + pageIndex.ToString() + "/");
            sb.Append(pageCount.ToString());
            sb.Append("&nbsp;每页" + pageSize.ToString() + "条");
            return sb.ToString();
        }
        private static void BindDDL(DropDownList ddl, DataTable dt, string textField, string valueField)
        {
            ddl.Items.Clear();
            ListItem li = new ListItem();
            li.Text = "请选择";
            li.Value = "-1";
            ddl.Items.Add(li);
            foreach (DataRow var in dt.Rows)
            {
                ListItem item = new ListItem();
                item.Text = var[textField].ToString();
                item.Value = var[valueField].ToString();
                ddl.Items.Add(item);
            }
        }

        public static void Bind_DLL_RoleList(DropDownList ddl)
        {
            BindDDL(ddl, new Role().GetList("IsEnable = 1").Tables[0], "Name", "Id");
        }
        /// <summary>
        /// 民族
        /// </summary>
        /// <param name="ddl"></param>
        public static void Bind_DLL_NationalityList(DropDownList ddl)
        {
            ddl.Items.Clear();
            DataTable dt =new Dic_Nationality().GetList(100, "IsDelete = 0", "id").Tables[0];
            ListItem li = new ListItem();
            li.Text = "请选择";
            li.Value = "-1";
            ddl.Items.Add(li);
            foreach (DataRow var in dt.Rows)
            {
                ListItem item = new ListItem();
                item.Text = var["Name"].ToString();
                item.Value = var["Name"].ToString();
                ddl.Items.Add(item);
            }
        }
        public static void Bind_DLL_ProvinceList(DropDownList ddl)
        {
            BindDDL(ddl, new T_Province().GetList(100, "IsEnable = 1", "SortNum").Tables[0], "Name", "Id");
        }

        public static void Bind_DLL_CityList(DropDownList ddl, string provId)
        {
            DataTable dt;
            if (provId == "-1")
            {
                dt = new DataTable();
            }
            else
            {
                dt = new T_City().GetList(1000, "IsEnable = 1 and ProID = " + provId, "SoreNum").Tables[0];
            }
            BindDDL(ddl, dt, "Name", "Id");
        }

        public static void Bind_DLL_DistrictList(DropDownList ddl, string cityId)
        {
            DataTable dt;
            if (cityId == "-1")
            {
                dt = new DataTable();
            }
            else
            {
                dt = new T_District().GetList(10000, "IsEnable = 1 and CityID = " + cityId, "SortNum").Tables[0];
            }
            BindDDL(ddl, dt, "Name", "Id");
        }
    }
}
